<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>编辑产品editProduct</title>
	</head>
	<link rel="stylesheet" href="../css/common.css" />
	<link rel="stylesheet" href="../css/less/common_less.css" />
	<link rel="stylesheet" href="../css/newBuiltProduct.css" />
	<style>
		body{
			
		}
		input{
			font-family:"微软雅黑";
		}
		table{
			width:1090px;
		}
		.delete{
			font-size:14px;
			margin-right:10px;
		}
		.showAndHideP{
			width:1088px;
		}
		.upCon{
			margin:10px 0 0 4px;
		}
		.addSchedule{
			display:none;
		}
		.upCon>li{
			height:50px;
			background:white;
			line-height:50px;
		}
		.upCon>li span{
			margin-left:14px;
		}
		.upCon>li>input{
			margin-left:20px;
			border:none;
			outline:none;
			width:500px;
			height:24px;
		}
		.flightList{
			padding-bottom:20px;
			background:white;
		}
	</style>
	<body>
		<p class='title f60666'>
			<span><i class='quare'></i>选择航班</span>	
					
		</p>
		<div class='flightList'>
				
		</div>
		<div class="upCon">
			<li><span>升舱</span><input class="seatUp" type="text" placeholder="请输入升舱信息"/></li>
			<li style="margin-top:5px;"><span>联运</span><input class="connectFltInfo" type="text" placeholder="请输入联运信息"/></li>
		</div>
		<p class='title scheduleTitle f60666'>
			<span><i class='quare'></i>组合排期&nbsp;&nbsp;&nbsp;<input class='asWeek' value="wk" type='radio' name="choose"/>按星期组合&nbsp;&nbsp;&nbsp;<input class='asInterval' value='iv' type='radio' name="choose"/>按间隔组合&nbsp;&nbsp;&nbsp;<input class='autoRuleInput' value='AUTO' type='radio' name="choose"/>自动排期</span>
			<span class='addSchedule'><i class='iconfont faa000' title=添加>&#xe627;</i></span>			
		</p>
		<div class='schedule'>
			<table class='scheduleTable_1'>
					<tr class='f868686'>	
						<th>排期编号</th>					
						<th>本航程</th>
						<th>间隔周数</th>
						<th>下一航程</th>
						<th>操作</th>
					</tr>
					
				</table>
				<table class='scheduleTable_2'>
					<tr class='f868686'>
						<th>第1程起始</th>
						<th>去程间隔天数</th>
						<th>回程间隔天数</th>
					</tr>
					<tr class='f60666'>
						<td><input class='thisDate thisDate_' type='text' class='datespicker' onFocus="new WdatePicker({dateFmt:'yyyy-MM-dd'})"/></td>
						<td><input class='itval itval_' type='number' value="1" min='0'/></td>
						<td><input class='nextDate nextDate_' type='number' value="1" min='0'/></td>
					</tr>
				</table>
				<div class='scheduleTable_3 f868686'>
					<span>规则<input class='autoRule' type='text'/></span>
				</div>		
			</div>
		</div>
				
		<p class='title f60666'>
			<span><i class='quare'></i>运价方案</span>
		</p>
		<div class='pricePlan f868686'>
			<p>
				<b style="color:#00aa98;">票损运价</b>
				—试用运价方案编号<span class='pricePlanNo'></span>
				<span class='pricePlanSomeDo'>
					<i class='iconfont faa000 addPricePlan' title='修改' pricePlan="ticketLoss">&#xe681;</i>
				</span>
			</p>
		</div>
		<div class='pricePlan f868686'>
			<p>
				<b style="color:#00aa98;">免票运价</b>
				—试用运价方案编号<span class='freePricePlanNo'></span>
				<span class='pricePlanSomeDo'>
					<i class='delFreePricePlan iconfont' title='删除'>&#xe606;</i>
					<i class='iconfont faa000 addPricePlan' title='修改' pricePlan="free">&#xe681;</i>
				</span>
			</p>
		</div>
		<div class='anniu'>
			<button class='Dolo_btn sure'>确定</button>
			<button class='Dolo_btn cancle'>取消</button>
		</div>
	</body>
		<script type="text/javascript" src="../js/jquery-1.12.4.js" ></script>
		<script type="text/javascript" src="../js/common.js" ></script>
		<script type="text/javascript" src="../layer-v2.4/layer/layer.js" ></script>
			<!-- 引入my97日期时间控件 -->
		<script type="text/javascript" src="../js/My97DatePicker/WdatePicker.js" charset="utf-8"></script>
		<script>
			
		
			
			//页面渲染
			var proId = getHref().proId
			var flightType = getHref().flightType
			$.ajax({
				type:"get",
				url:"../product/"+proId+"/showdetail",				
				async:false,
				success:function(data){	
					data = JSON.parse(data)
					var No = 0;
					for(var i=0;i<data.data.productItineraryVo.length;i++){	
						var str="";
						No++
						str+="<div class='flight'>"+
								"<div class='flight_Content'>"+
								"<p>"+
									"<span class='smallTitle f868686'> 第  "+No+" 程航班</span>"+
									"<span class='do'><i class='delete iconfont faa000' title=删除>&#xe606;</i><i class='add iconfont faa000' title=添加>&#xe661;</i>"+
								"</p>"+
								"<table>"+
									"<thead><tr class='f868686'>"+
										"<th>航班号</th>"+
										"<th>出发机场</th>"+
										"<th>到达机场</th>"+
										"<th>出发日期</th>"+
										"<th>到达日期</th>"+
										"<th>出发时间</th>"+
										"<th>到达时间</th>"+
										"<th>班期</th>"+
									"</tr></thead><tbody>"
									
						for(var j=0;j<data.data.productItineraryVo[i].length;j++){
							str+="<tr class='f60666'>" +
									 "<td>"+data.data.productItineraryVo[i][j].airlineCode+"</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].depCn+"("+data.data.productItineraryVo[i][j].depAirport+")</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].arrCn+"("+data.data.productItineraryVo[i][j].arrAirport+")</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].availableDate+"</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].unavailableDate+"</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].depTime+"</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].arrTime+"</td>" +
									 "<td>"+data.data.productItineraryVo[i][j].schedule+"</td>" +
									 "<td style=display:none; class=postId>"+data.data.productItineraryVo[i][j].fltSegId+"</td>" +
								 "</tr>" 						
						}			
							str+="</tbody></table>"+
								 "<p class='showAndHideP faa000'><span class=showAndHide>查看更多<span></p>"+
							"</div>"
						if(data.data.returnType=="MW"){
							if(i==1){
								str+="<div class='MWinterval'>选择间隔天数<select class='fi'>" +
											"<option value='0'>0</option>" +
											"<option value='1'>1</option>" +
											"<option value='2'>2</option>" +
											"<option value='3'>3</option>" +
											"<option value='4'>4</option>" +
											"<option value='5'>5</option>" +
											"<option value='6'>6</option>" +
											"<option value='7'>7</option>" +
										"</select>" +
									 "<div>"
							}
							if(i==3){
								str+="<div class='MWinterval'>选择间隔天数<select class='si'>" +
											"<option value='0'>0</option>" +
											"<option value='1'>1</option>" +
											"<option value='2'>2</option>" +
											"<option value='3'>3</option>" +
											"<option value='4'>4</option>" +
											"<option value='5'>5</option>" +
											"<option value='6'>6</option>" +
											"<option value='7'>7</option>" +
										"</select>" +
									 "<div>"
							}
						}	
						str+="</div>";
						$(".flightList").append(str);
						hideOthersTr();	//隐藏除了前两个tr外的其他的tr;																	
					}
					//组合排期
					if(data.data.returnType=="MW"){
						$(".si").val(data.data.si);
						$(".fi").val(data.data.fi);
						$(".asInterval").attr("disabled",true);
						$(".autoRuleInput").attr("disabled",true);
					}
					if(data.data.schType=="WK"){
						var len = data.data.productScheduleVo.length;
						$(".scheduleTable_2").hide();
						$(".scheduleTable_3").hide();
						$(".addSchedule").show();
						$(".asWeek").prop("checked","checked")						
						for(var n=0;n<len;n++){
							addSchedule()
							countNumber()
							$(".scheduleTable_1 .itval").eq(n).val(data.data.productScheduleVo[n].itval)
							$(".scheduleTable_1 .nextDate").eq(n).val(data.data.productScheduleVo[n].nextData)
							$(".scheduleTable_1 .thisDate").eq(n).val(data.data.productScheduleVo[n].thisDate) 	
						}
					}if(data.data.schType=="IV"){
						$(".scheduleTable_1").hide();
						$(".scheduleTable_3").hide();
						$(".asInterval").prop("checked","checked")
						$(".itval_").val(data.data.productScheduleVo[0].itval)
						$(".nextDate_").val(data.data.productScheduleVo[0].nextData)
						$(".thisDate_").val(data.data.productScheduleVo[0].thisDate)
					}if(data.data.schType=="AUTO"){
						$(".autoRuleInput").prop("checked","checked")
						$(".scheduleTable_1").hide();
						$(".scheduleTable_2").hide();
						var autoThisDate = JSON.parse(data.data.productScheduleVo[0].thisDate).join("/");																	
						$(".autoRule").val(autoThisDate);
					}if(!data.data.schType){
						$(".scheduleTitle").hide();
						$(".schedule").hide();
					}
					//运价方案
					$(".pricePlanNo").text(data.data.fareName)

					//免票运价方案
					if(data.data.freeFareId){
						$(".freePricePlanNo").text(data.data.freeFareId)
					}

					//联运
					if(data.data.connectFltInfo){
						$(".connectFltInfo").val(data.data.connectFltInfo);
					}
					//升舱
					if(data.data.seatUp){
						$(".seatUp").val(data.data.seatUp);
					}
					
				}
			})
					
		




		//_________________________________点击添加 选择航班_________________________________________
			var flightTableIndex
		$(".flightList").delegate(".add","click",function(){
			flightTableIndex = $(this).parent().parent().parent().parent().index();
			layer.open({
				type: 2,
				title: ['选择航班','background-color:#6d6e72; color:#fff;font-size:14px;'],
				shadeClose: true,
				shade: [0.9,'#cfcfcf'],
				area: ['100%', '100%'],  
				content: ['../ak/chooseFlight.html'],// iframe的url，no代表不显示滚动条
				end:function(){
					
				}			
			});
		})
		//_________________________________点击删除 清空表格中的表体______________________________________
		$(".flightList").delegate(".delete","click",function(){
			$(this).parent().parent().next().children("tbody").empty();
			
		})
		
		//_________________________________点击添加  选择运价方案_________________________________________
		$(".addPricePlan").click(function(){
			var strUrl = "";
			var strTitle = "";
			if($(this).attr("pricePlan")=="free"){
				strUrl = '../ak/choosePricePlan.html?free=free';
				strTitle = "选择免票运价方案";
			}else{				
				strUrl = '../ak/choosePricePlan.html?';
				strTitle = "选择运价方案";
			}
			layer.open({
				type: 2,
				title: [strTitle,'background-color:#6d6e72; color:#fff;font-size:14px;'],
				shadeClose: true,
				shade: [0.9,'#cfcfcf'],
				area: ['100%', '100%'],  
				content: [strUrl],// iframe的url，no代表不显示滚动条
				end:function(){
					
				}			
			});
		})
		//_________________________________点击添加航程_________________________________________
		$(".addFlightRoute").click(function(){
			addFlightMess()			
		})
		//____________________________________取消按钮_______________________________________
		$(".cancle").click(function(){
			var index = parent.layer.getFrameIndex(window.name);//获取弹窗索引
			parent.layer.close(index);//关闭弹窗
		})

		//____________________________________选择航班页面调用的父级页面的方法________________________
		
		function setData(data){ 
				console.log(data)
				creatTableOrNot();         //判断是否生成table
				var lengthOld = $(".flight_Content").eq(flightTableIndex).find("table tr").length;//获得已有的tr个数
				for(var i=0;i<data.length;i++){   //判断生成多少个tr
					creatTr()		
				}
				var lengthNew = $(".flight_Content").eq(flightTableIndex).find("table tr").length;//获得所有的tr个数
				var dataIndex = 0;//数据数组的索引
				for(var j=lengthOld;j<lengthNew;j++){	
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(0).text(data[dataIndex].flightNo)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(1).text(data[dataIndex].depAirport)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(2).text(data[dataIndex].arrAirport)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(3).text(data[dataIndex].DepDate)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(4).text(data[dataIndex].ArrDate)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(5).text(data[dataIndex].depTime)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(6).text(data[dataIndex].arrTime)
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(7).text(data[dataIndex].dayofweek)	
					$(".flight_Content").eq(flightTableIndex).find("table tr").eq(j).find("td").eq(8).text(data[dataIndex].id)	
					dataIndex++
				}
				hideOthersTr();  //隐藏tr信息
				$(".flight_Content").eq(flightTableIndex).find("span[class=showAndHide]").text("查看更多");//避免在展开情况下，添加flight导致按钮颠倒
		}
		//____________________________________选择运价方案页面调用的父级页面的方法________________________

		function choosePlan(data){
			$(".pricePlanNo").text(data)
		}
		function choosePlanFree(data){
			$(".freePricePlanNo").text(data)
		}
		//____________________________________修改产品提交________________________
		$(".sure").click(function(){
			var index
			var submitMess = {};
			submitMess.type = flightType;
			submitMess.productItineraries = getFlightNo();	
			submitMess.schType = $("input[type='radio']:checked").val();
			submitMess.productSchedules = [];		

			submitMess.freeFareId = $(".freePricePlanNo").text();
			submitMess.fareId = $(".pricePlanNo").text();	
			submitMess.seatUp = $(".seatUp").val();
			submitMess.connectFltInfo = $(".connectFltInfo").val();
			if(flightType=="MW"){
				submitMess.si = $(".si").val();
				submitMess.fi = $(".fi").val();
			}			
			if(submitMess.schType=="wk"){
				var len = $(".scheduleNo").length;
				for(var i=0;i<len;i++){
					var json={};
					json.itval = $(".scheduleTable_1 .itval").eq(i).val();				
					json.nextData = $(".scheduleTable_1 .nextDate").eq(i).val();
					json.thisDate = $(".scheduleTable_1 .thisDate").eq(i).val();
					submitMess.productSchedules.push(json)
				}
			}		
			if(submitMess.schType=="iv"){
				var json = {};
				json.itval = $(".itval_").val();
				json.nextData = $(".nextDate_").val();
				json.thisDate = $(".thisDate_").val();
				if(json.itval==""||json.nextData==""||json.thisDate==""){
					layer.msg("请添加完整的组合排期！",{icon:2,time:1000})
					return false;
				}
				submitMess.productSchedules.push(json)
			}if(submitMess.schType=="AUTO"){
				var json = {};
				json.itval = "";
				json.nextData = "";
				json.thisDate = $(".autoRule").val().split("/");;
				if(json.thisDate==""){
					layer.msg("自动排期不能为空",{icon:2,time:1000})
					return false;
				}
				submitMess.productSchedules.push(json)
			}
			layer.load();
			$.ajax({
				type:"post",
				url:"../product/"+proId+"/update?temp="+Math.random(),
				contentType:'application/json;charset=utf-8',
				async:true,
				data:JSON.stringify(submitMess),
				success:function(data){
					layer.closeAll('loading');
					data = JSON.parse(data)
					if(data.status=="1001"){
						layer.msg('修改产品成功', {icon: 1,time:1000},function(){
							var index = parent.layer.getFrameIndex(window.name);//获取弹窗索引
							parent.layer.close(index);//关闭弹窗
						})
					}if(data.status=="1002"){						
						//失败的弹出框
						layer.confirm(data.message, {
							title:['修改产品失败','background-color:#6d6e72;color:#fff;font-size:14px;'],
						   	btn: ['确定'] //按钮
						}, function(){
							layer.closeAll('dialog');
							return false;
						}); 
					}
									
				}			
			});	
		})



	
		//______________________________________________________________________________排期选择
		$(".asWeek").click(function(){
			$(".scheduleTable_1").show();
			$(".addSchedule").show();
			$(".scheduleTable_2").hide();
			$(".scheduleTable_3").hide();
		})
		$(".asInterval").click(function(){
			$(".scheduleTable_1").hide();
			$(".addSchedule").hide();
			$(".scheduleTable_2").show();
			$(".scheduleTable_3").hide();
		})
		$(".autoRuleInput").click(function(){
			$(".scheduleTable_1").hide();
			$(".addSchedule").hide();
			$(".scheduleTable_2").hide();
			$(".scheduleTable_3").show();
		})

	//_______________________________________________________________________________删除排期
		$(".scheduleTable_1").delegate(".deleteSchedule","click",function(){
			if($(".deleteSchedule").length>1){
				$(this).parent().parent().remove();
				countNumber();
			}
		})
		
		
		
		
	//_______________________________________________________________________________添加排期
		$(".addSchedule").click(function(){		
			addSchedule();
			countNumber();
		})
	//———————————————————————————————————————————————————————————————————————————————添加排期函数
		function addSchedule(){
			var str="";
			str+="<tr class='f60666'>"+
							"<td class='scheduleNo'></td>"+
							"<td style='width:250px;'>"+
								"<select class='thisDate' style='width:100%;height:100%;border:none;font-family:微软雅黑;padding-left:100px;'>"+
									"<option value=1>星期一</option>"+
									"<option value=2>星期二</option>"+
									"<option value=3>星期三</option>"+
									"<option value=4>星期四</option>"+
									"<option value=5>星期五</option>"+
									"<option value=6>星期六</option>"+
									"<option value=7>星期日</option>"+
								"</select>"+
							"</td>"+
							"<td style='width:250px;'>"+
								"<select class='itval' style='width:100%;height:100%;border:none;font-family:微软雅黑;padding-left:100px;'>"+
									"<option value=0>同周</option>"+
									"<option value=1>下一周</option>"+
									"<option value=2>下二周</option>"+
									"<option value=3>下三周</option>"+
									"<option value=4>下四周</option>"+
									"<option value=5>下五周</option>"+
									"<option value=6>下六周</option>"+
									"<option value=7>下七周</option>"+
								"</select>"+
							"</td>"+
							"<td style='width:250px;'>"+
								"<select class='nextDate' style='width:100%;height:100%;border:none;font-family:微软雅黑;padding-left:100px;'>"+
									"<option value=1>星期一</option>"+
									"<option value=2>星期二</option>"+
									"<option value=3>星期三</option>"+
									"<option value=4>星期四</option>"+
									"<option value=5>星期五</option>"+
									"<option value=6>星期六</option>"+
									"<option value=7>星期日</option>"+
								"</select>"+
							"</td>"+
							"<td><span class='deleteSchedule'>删除</span></td>"+
						"</tr>"
			$(".scheduleTable_1").append(str)
		}
	//———————————————————————————————————————————————————————————————————————————————序号计算
		function countNumber(){
			var len = $(".scheduleNo").length;
			for(var i=0;i<len;i++){
				$(".scheduleNo").eq(i).text(i+1)
			}
		}

		//———————————————————————————————————————————————————————————————————————————————获取数据后生成table
		function creatTable(){
			var str = "";
			str+="<table>" +
					"<tr>" +
						"<th>航班号</th>" +
						"<th>出发机场</th>" +
						"<th>到达机场</th>" +
						"<th>出发日期</th>" +
						"<th>到达日期</th>" +
						"<th>出发时间</th>" +
						"<th>到达时间</th>" +
						"<th>班期</th>" +
					 "</tr>" +
				 "</table>"
			$(".flight_Content").eq(flightTableIndex).append(str)
		}
	//———————————————————————————————————————————————————————————————————————————————获取数据后生成添加tr到table
		function creatTr(){
			var str = "";
			str+="<tr>" +
					"<td></td>" +
					"<td></td>" +
					"<td></td>" +
					"<td></td>" +
					"<td></td>" +
					"<td></td>" +
					"<td></td>" +
					"<td></td>" +
					"<td style=display:none; class=postId></td>" +
				 "</tr>"
			$(".flight_Content").eq(flightTableIndex).find("table").append(str)
		}
	//———————————————————————————————————————————————————————————————————————————————判断是否需要生成table
		function creatTableOrNot(){
			if($(".flight_Content").eq(flightTableIndex).find("table").length==0){
				creatTable()			
				$(".flight_Content").eq(flightTableIndex).css({"padding-bottom":"20px"})
			}
		}

	//———————————————————————————————————————————————————————————————————————————————获取每table中class为postId的td的值
		
		function getFlightNo(){
			var allIdArr = [];
			var tableLength = $(".flight").length;
			for(var i=0;i<tableLength;i++){
				var trLength = $(".flight").eq(i).find("tr").length;
				var idArr = [];
				for(var j=0;j<trLength-1;j++){
					var postId = $(".flight").eq(i).find("tr").find("td[class=postId]").eq(j).text();
					idArr.push(postId)
				}
				allIdArr.push(idArr)
			}
			return allIdArr;
		}
//————————————————————————————————————————————————————————————————————————————————隐藏除了前两个tr外的其他tr
		
		function hideOthersTr(){
			var lengthTable = $(".flightList table").length;
			for(var i=0;i<lengthTable;i++){
				var lengthTr = $(".flightList table").eq(i).find("tr").length;		
				for(var j=2;j<lengthTr;j++){
					$(".flightList table").eq(i).find("tr").eq(j).hide();
				}
			}
		}
//————————————————————————————————————————————————————————————————————————————————查看更多
		$(".flightList").delegate(".showAndHide","click",function(){			
			var length = $(this).parent().parent().find("table tr").length;
			for(var i=2;i<length;i++){
				$(this).parent().parent().find("table tr").eq(i).toggle();
			}								
			if($(this).text()=="查看更多"){
				$(this).text("收起")
			}else{
				$(this).text("查看更多")
			}
		})
//________________________________________________________________________________删除免票运价
	$(".delFreePricePlan").click(function(){
		$(".freePricePlanNo").text("");
	})


		
		</script>	
</html>
